package com.google.code.fluentjoiner.example;

import com.google.code.fluentjoiner.IteratorSupplierBuilder;
import com.google.code.fluentjoiner.io.http.HttpFileObjectSupplierBuilder;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import java.util.Iterator;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class GoogleTest
{
    private static final Logger LOGGER = LoggerFactory.getLogger(GoogleTest.class);
    
    public static void main(final String[] args)
    {
        LOGGER.info("Starting...");
        final ImmutableList.Builder<String> urlsBuilder = ImmutableList.builder();
        for (int count = 0; count < 10; count++)
        {
            urlsBuilder.add("http://www.google.com");
        }
        final ImmutableList<String> urls = urlsBuilder.build();

        final ExecutorService executorService = Executors.newFixedThreadPool(4);
        final Iterator<String> pageContents = IteratorSupplierBuilder.fromIterable(urls)
                .parallelElementTransform(new Function<String, String>()
                {
                    @Override
                    public String apply(final String from)
                    {
                        return HttpFileObjectSupplierBuilder.fromPath(from).textFromFile().get();
                    }
                }, executorService)
                .get();

        LOGGER.debug("Results received.");
        final ImmutableList<String> resultList = ImmutableList.copyOf(pageContents);
        for(final String result : resultList)
        {
            LOGGER.info("Value length: {}", result.length());
        }
        executorService.shutdown();
        LOGGER.info("End of line.");
    }
}